package com.ruoyi.cangku.service.impl;

import com.ruoyi.cangku.domain.Shangpinguanli;
import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.cangku.service.IShangpinguanliService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * sahngpinService业务层处理
 *
 * @author ruoyi
 * @date 2023-11-07
 */
@Service
public class ShangpinguanliServiceImpl implements IShangpinguanliService {
    @Autowired
    private ShangpinguanliMapper shangpinguanliMapper;

    /**
     * 查询sahngpin
     *
     * @param id sahngpin主键
     * @return sahngpin
     */
    @Override
    public Shangpinguanli selectShangpinguanliById (Integer id) {
        return shangpinguanliMapper.selectShangpinguanliById(id);
    }

    /**
     * 查询sahngpin列表
     *
     * @param shangpinguanli sahngpin
     * @return sahngpin
     */
    @Override
    public List<Shangpinguanli> selectShangpinguanliList (Shangpinguanli shangpinguanli) {
        return shangpinguanliMapper.selectShangpinguanliList(shangpinguanli);
    }

    /**
     * 新增sahngpin
     *
     * @param shangpinguanli sahngpin
     * @return 结果
     */
    @Override
    public int insertShangpinguanli (Shangpinguanli shangpinguanli) {
        if (Objects.equals(shangpinguanli.getShangpinleibie(), "防火锁")
                || Objects.equals(shangpinguanli.getShangpinleibie(), "闭门器")
                || Objects.equals(shangpinguanli.getShangpinleibie(), "拉爆盖")) {
            Shangpinguanli shangpinguanli1 = new Shangpinguanli();
            shangpinguanli1.setShangpinleibie(shangpinguanli.getShangpinleibie());
            List<Shangpinguanli> shangpinguanliList = shangpinguanliMapper.selectShangpinguanliList(shangpinguanli1);
            if (shangpinguanliList.size() == 0) {
                return shangpinguanliMapper.insertShangpinguanli(shangpinguanli);
            } else {
                return 0;
            }
        } else {
            Shangpinguanli shangpinguanli1 = new Shangpinguanli();
            BeanUtils.copyProperties(shangpinguanli, shangpinguanli1);
            shangpinguanli1.setShangpintiaoma(null);
            shangpinguanli1.setShangpintupian(null);
            List<Shangpinguanli> shangpinguanliList = shangpinguanliMapper.selectShangpinguanliList(shangpinguanli1);
            if (shangpinguanliList.size() == 0) {
                return shangpinguanliMapper.insertShangpinguanli(shangpinguanli);
            } else {
                return 0;
            }
        }
    }

    /**
     * 修改sahngpin
     *
     * @param shangpinguanli sahngpin
     * @return 结果
     */
    @Override
    public int updateShangpinguanli (Shangpinguanli shangpinguanli) {
        return shangpinguanliMapper.updateShangpinguanli(shangpinguanli);
    }

    /**
     * 批量删除sahngpin
     *
     * @param ids 需要删除的sahngpin主键
     * @return 结果
     */
    @Override
    public int deleteShangpinguanliByIds (String ids) {
        return shangpinguanliMapper.deleteShangpinguanliByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除sahngpin信息
     *
     * @param id sahngpin主键
     * @return 结果
     */
    @Override
    public int deleteShangpinguanliById (Integer id) {
        return shangpinguanliMapper.deleteShangpinguanliById(id);
    }

    @Override
    public Shangpinguanli selectShangpinxinxiByDanhao(String code,int id) {
        Shangpinguanli shangpinguanli = new Shangpinguanli();
        shangpinguanli.setShangpinbianhao(code);
        shangpinguanli.setId(Math.toIntExact(id));
        List<Shangpinguanli> shangpinguanliList = shangpinguanliMapper.selectShangpinguanliList(shangpinguanli);
        if (shangpinguanliList.size() == 0) {
            return null;
        }
        return shangpinguanliList.get(0);
    }
}
